package axht.trade.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MenuActivity extends AppCompatActivity {

    private Button btn_test2_show_cm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        btn_test2_show_cm = findViewById(R.id.btn_test2_show_cm);
        //设置创建上下文菜单的监听
        btn_test2_show_cm.setOnCreateContextMenuListener(this);
    }


    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        //添加菜单项
        menu.add(0,1,0,"添加");
        menu.add(0,4,0,"删除");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case 1:
                Toast.makeText(this,"添加",Toast.LENGTH_LONG).show();
                break;
            case 4:
                Toast.makeText(this,"删除",Toast.LENGTH_LONG).show();
                break;
            default:
                break;
        }
        return super.onContextItemSelected(item);
    }

    /**
     * 用来显示optionmenu的方法:向menu中添加Item
     * @param menu
     * @return
     */
    /*@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // 纯编码方式
        menu.add(0,2,0,"添加");
        menu.add(0,3,0,"删除");
        return super.onCreateOptionsMenu(menu);
    }*/

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        //菜单文件方式
        //1 得到菜单加载器对象
        MenuInflater menuInflater = getMenuInflater();
        //2 加载菜单文件
        menuInflater.inflate(R.menu.option_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            //case 2:
            case R.id.add:
                Toast.makeText(this,"添加",Toast.LENGTH_LONG).show();;
                break;
            //case 3:
            case R.id.delete:
                Toast.makeText(this,"删除",Toast.LENGTH_LONG).show();;
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
